package cn.mengfly.fxtest.app;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Pair;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mengfly
 * @date 2021/5/27 19:07
 */
public class CombosApp extends Application {

    private final static Pair<String, String> EMPTY_PAIR = new Pair<>("", "");



    private final ComboBox<Pair<String, String>> account = new ComboBox<>();

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label accountLabel = new Label("Account:");
        account.setPrefWidth(200);
        Button saveButton = new Button("Save");

        HBox hBox = new HBox(accountLabel, account, saveButton);
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.setPadding(new Insets(40));

        Scene scene = new Scene(hBox);
        initCombo();

        saveButton.setOnAction(event -> {
            if (account.getValue().equals(EMPTY_PAIR)) {
                System.out.println("No save needed; no item selected");
            } else {
                System.out.println("Saving " + account.getValue());
            }
        });

        primaryStage.setTitle("CombosApp");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void initCombo() {
        List<Pair<String, String>> accounts = new ArrayList<>();

        accounts.add(new Pair<>("Auto Expense", "60000"));
        accounts.add(new Pair<>("Interest Expense", "61000"));
        accounts.add(new Pair<>("Office Expense", "62000"));
        accounts.add(new Pair<>("Salaries Expense", "63000"));

        account.getItems().add(EMPTY_PAIR);
        account.getItems().addAll(accounts);

        account.setValue(EMPTY_PAIR);

        Callback<ListView<Pair<String, String>>, ListCell<Pair<String, String>>> factory = param ->
                new ListCell<Pair<String, String>>() {
                    @Override
                    protected void updateItem(Pair<String, String> item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setText("");
                        } else {
                            setText(item.getKey());
                        }
                    }
                };
        account.setCellFactory(factory);
        account.setButtonCell(factory.call(null));

    }
}
